package yl.hs.bmipfull.service.dataCockpit.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.material.*;
import yl.hs.bmipfull.service.dataCockpit.IRawMaterialProcurementService;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.dataCockpit.PriceTrendVo;
import yl.hs.bmipfull.viewmodels.dataCockpit.RawMaterialProcurement;
import yl.hs.bmipfull.viewmodels.dataCockpit.WarehouseModel;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 张凯
 * @apiNote
 * @since 2022/9/19 10:43 创建
 */
@Service
public class RawMaterialProcurementServiceImpl implements IRawMaterialProcurementService {


    @Autowired
    IPurchaseReceiptDao dao;


    @Autowired
    IWarehouseDao warehouseDao;



    @Autowired
    IRawMaterialInventoryDao inventoryDao;

    /**
     * 原材采购
     *
     * @param inTime
     * @return
     */
    @Override
    public List<RawMaterialProcurement> ProcurementSituation(String inTime) {
        return dao.getProcurementSituation(inTime);

    }

    /**
     * 原材耗材
     *
     * @param outTime
     * @return
     */
    @Override
    public List<RawMaterialProcurement> consumables(String outTime) {

        return dao.getConsumables(outTime);

    }


    /**
     * 原材库存
     *
     * @return
     */
    @Override
    public Map<String,List<WarehouseModel>> RawMaterialsStock() {
        Map<String,List<WarehouseModel>> data = new HashMap<>();
        String glSN = HsApp.getSystemSet(CK.AggregateSN);
        String flSN = HsApp.getSystemSet(CK.PowderSN);

        if(!Tl.isEmpty(glSN)) {
            var list  = inventoryDao.queryListByCateSN(glSN);
            data.put("aggregates",list);
        }
        else {
            data.put("aggregates",new ArrayList<>());
        }
        if(!Tl.isEmpty(flSN)) {
            data.put("powder",inventoryDao.queryListByCateSN(flSN));
        }
        else {
            data.put("powder",new ArrayList<>());
        }
        return data;
    }


    @Override
    public Map<String, Object> getSandPrice() throws ParseException {
        //返回值设值
        Map<String, Object> result = new HashMap<>();
        Calendar cNow = Now();
        Calendar cLast = Last();
        //月份集合
        List<String> monthStr = new ArrayList<>();
        //本期每月重量
        List<BigDecimal> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<BigDecimal> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i > 0) {
                cNow.add(Calendar.MONTH, 1);
                cLast.add(Calendar.MONTH, 1);
            }
            monthStr.add((i + 1) + "月");
            var priceTrend = dao.getPriceTrend(cNow.getTime(), "砂子");
            var trend = dao.getPriceTrend(cLast.getTime(), "砂子");
            nowNetWeight.add(priceTrend);
            oldNetWeight.add(trend);

        }

        result.put("nowNetWeight", nowNetWeight);
        result.put("oldNetWeight", oldNetWeight);
        result.put("monthStr", monthStr);
        return result;
    }

  @Override
    public Map<String, Object> getStonePrice() throws ParseException {
      Calendar cNow = Now();
      Calendar cLast = Last();
      //返回值设值
        Map<String, Object> result = new HashMap<>();
        //月份集合
        List<String> monthStr = new ArrayList<>();
        //本期每月重量
        List<BigDecimal> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<BigDecimal> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i > 0) {
                cNow.add(Calendar.MONTH, 1);
                cLast.add(Calendar.MONTH, 1);
            }
            monthStr.add((i + 1) + "月");
            var priceTrend = dao.getPriceTrend(cNow.getTime(), "石料");
            var trend = dao.getPriceTrend(cLast.getTime(), "石料");
            nowNetWeight.add(priceTrend);
            oldNetWeight.add(trend);

        }

      result.put("nowNetWeight", nowNetWeight);
      result.put("oldNetWeight", oldNetWeight);
      result.put("monthStr", monthStr);
      return result;
    }

    @Override
    public Map<String, Object> getCementPrice()  {
        //返回值设值
        Map<String, Object> result = new HashMap<>();
        Calendar cNow = Now();
        Calendar cLast = Last();
        //月份集合
        List<String> monthStr = new ArrayList<>();
        //本期每月重量
        List<BigDecimal> nowNetWeight = new ArrayList<>();
        //上期每月重量
        List<BigDecimal> oldNetWeight = new ArrayList<>();
        //循环查询1-12月销售重量（本期与上期）
        for (int i = 0; i < 12; i++) {
            if (i > 0) {
                cNow.add(Calendar.MONTH, 1);
                cLast.add(Calendar.MONTH, 1);
            }
            monthStr.add((i + 1) + "月");
            var priceTrend = dao.getPriceTrend(cNow.getTime(), "水泥");
            var trend = dao.getPriceTrend(cLast.getTime(), "水泥");
            nowNetWeight.add(priceTrend);
            oldNetWeight.add(trend);


        }

     result.put("nowNetWeight", nowNetWeight);
        result.put("oldNetWeight", oldNetWeight);
        result.put("monthStr", monthStr);
        return result;
    }


    public  Calendar Now(){
        //今年1月，将cNow对象时间设置为本年一月份
        Calendar cNow = Calendar.getInstance();
        cNow.setTime(new Date());
        cNow.clear(Calendar.MONTH);
        return cNow;
    }

    public   Calendar Last(){
        //去年1月,将cLast对象时间设置为本年一月份
        Calendar cLast = Calendar.getInstance();
        cLast.setTime(new Date());
        cLast.clear(Calendar.MONTH);
        cLast.add(Calendar.YEAR, -1);
        return cLast;
    }



}
